﻿using System;
using StackNamespace;
using LoggerNamespace;

namespace PresentationNamespace
{

    public class StackPresenter
    {
        IStackView view;
        Stack stack;
        ILogger log;

        public StackPresenter(IStackView _view)
        {
            view = _view;
            stack = new Stack();
            log = new LoggerText();

            view.OnAddElementClick += AddElement;
            view.OnGetElementClick += GetElement;
            view.OnSaveLogClick += SaveLog;
            view.OnShowLogClick += ShowLog;
            view.OnChangeTypeLogToHtml += ChangeTypeLogToHtml;
            view.OnChangeTypeLogToText += ChangeTypeLogToText;

            SetDefaultValueForAllField();
            view.isLogPanelVisible = false;

            log.AddMessage("Stack is create");
            PrintLogToWindowLog();
            view.typeLog = TypeLog.Text;
        }

        private void AddElement()
        {
            stack.AddValue( view.addItemField );

            log.AddMessage("Push new element " + view.addItemField + " to stack");
            PrintLogToWindowLog();

            SetDefaultValueForAllField();
            RefreshStackState();
        }

        private void RefreshStackState()
        {
            string newStackState = "";
            for(int i = (stack.GetLength() - 1); i >= 0 ; i--)
            {
                newStackState += stack.ReadValueByIndex(i).ToString();
                newStackState += "\n";
            }
            view.stackState = newStackState;
        }

        private void SetDefaultValueForAllField()
        {
            view.addItemField = "";
            view.gotItemField = "";
            view.stackState = "";
            view.isErrorMessageVisible = false;
        }

        private void PrintLogToWindowLog()
        {
            if (view.isLogPanelVisible)
            {
                view.logWindow = log.ShowLog();
            }
        }

        private void GetElement()
        {
            Object element;
            try
            {
                SetDefaultValueForAllField();
                element = stack.GetValue();
                log.AddMessage("Pop element " + element.ToString() + " from stack");
                view.gotItemField = element.ToString();
                RefreshStackState();
            }
            catch (Exception e)
            {
                view.errorMessage = e.Message;
                view.isErrorMessageVisible = true;
                log.AddMessage("Error: " + e.Message);
            }
            finally
            {
                PrintLogToWindowLog();
            }
        }

        private void ShowLog()
        {
            view.isLogPanelVisible = !view.isLogPanelVisible;
            PrintLogToWindowLog();
        }

        private void SaveLog()
        {
            log.SaveLog();
        }
        private void ChangeTypeLogToHtml()
        {
            if (view.typeLog == TypeLog.Html)
            {
                return;
            }
            LoggerHtml loggerHtml = (LoggerHtml)(LoggerText)log;
            log = loggerHtml;
            view.typeLog = TypeLog.Html;
            PrintLogToWindowLog();
        }
        private void ChangeTypeLogToText()
        {
            if (view.typeLog == TypeLog.Text)
            {
                return;
            }
            LoggerText loggerText = (LoggerText)(LoggerHtml)log;
            log = loggerText;
            view.typeLog = TypeLog.Text;
            PrintLogToWindowLog();
        }
    }
}
